package scenes;

import images.GameImagesMap;

import java.util.ArrayList;
import java.util.List;

import com.uqbar.vainilla.appearances.Sprite;
import components.LifeCounter;
import components.ball.NormalBall;
import components.blocks.Block;
import components.borders.Border;
import components.borders.DeadBorder;
import components.decoration.Background;
import components.paddle.NormalPaddle;
import components.paddle.Paddle;
import components.score.ScoreLabel;

public class MainGameScene extends CollisionablesScene {
	
	private List<NormalBall> ballsOnGame;
	private Paddle paddle;
	private LifeCounter lifeCounter;
	private ScoreLabel scoreGc;
	private int blockCount;



	public MainGameScene(int initialLifes, int initialScore){
		super(20, 800, 600);
		this.ballsOnGame = new ArrayList<NormalBall>();
		this.setBlockCount(0);
		this.addPaddle();
		this.addBall();
		this.addBorders();
		this.addBlocks();
		this.addLifeCounter(initialLifes);
		this.addScoreLabel(initialScore);
		this.addMainBackground();
		this.addSideBackground();
	}

	protected void addSideBackground() {
		Sprite sp = GameImagesMap.getImage("verticalborder");
		sp = sp.scaleTo(200, 600);
		Background bg = new Background(sp, 800, 0);
		this.addComponent(bg);
	}

	protected void addMainBackground() {
		
	}

	private void addScoreLabel(int initialScore) {
		this.scoreGc = new ScoreLabel(initialScore);
		this.scoreGc.setX(850);
		this.scoreGc.setY(150);
		this.addComponent(this.scoreGc);
	}

	private void addLifeCounter(int initialLifes) {
		this.lifeCounter =new LifeCounter(initialLifes);
		this.lifeCounter .setX(850);
		this.lifeCounter .setY(100);
		this.addComponent(this.lifeCounter );	
	}

	private void addBall() {
		this.addBall(new NormalBall(400, 200, this.getPaddle()));
	}

	protected void addBlock(Block block){
		this.addCollisionable(block);
		this.setBlockCount(this.getBlockCount() +1);
	}
	public void addBall(NormalBall ball) {
		this.getBallsOnGame().add(ball);
		this.addCollisionable(ball);
	}
	protected void addBlocks() {
//		this.addCollisionable(new Block(Color.BLACK, 200, 200));
//		this.addCollisionable(new Block(Color.BLACK, 50, 50));
//		this.addCollisionable(new Block(Color.BLACK, 700, 50));
//		this.addCollisionable(new Block(Color.BLACK, 50, 300));
//		this.addCollisionable(new Block(Color.BLACK, 250, 200));
//		this.addCollisionable(new Block(Color.BLACK, 300, 200));
//		this.addCollisionable(new Block(Color.BLACK, 350, 200));
//		this.addCollisionable(new Block(Color.BLACK, 400, 200));
//		this.addCollisionable(new Block(Color.BLACK, 450, 200));
//		this.addCollisionable(new Block(Color.BLACK, 500, 200));
//		this.addCollisionable(new Block(Color.BLACK, 550, 200));
//		this.addCollisionable(new Block(Color.BLACK, 600, 200));
//		this.addCollisionable(new Block(Color.BLACK, 200, 225));
//		this.addCollisionable(new Block(Color.BLACK, 250, 225));
//		this.addCollisionable(new Block(Color.BLACK, 300, 225));
//		this.addCollisionable(new Block(Color.BLACK, 350, 225));
//		this.addCollisionable(new Block(Color.BLACK, 400, 225));
//		this.addCollisionable(new Block(Color.BLACK, 450, 225));
//		this.addCollisionable(new Block(Color.BLACK, 500, 225));
//		this.addCollisionable(new Block(Color.BLACK, 550, 225));
//		this.addCollisionable(new Block(Color.BLACK, 600, 225));
	}

	private void addBorders() {
		Border topBorder, rightBorder, leftBorder;
		topBorder = new Border(800, 30, 0, 0, GameImagesMap.getImage("horizontalborder"));
		rightBorder = new Border(30, 600, 770, 0, GameImagesMap.getImage("verticalborder"));
		leftBorder = new Border(30, 600, 0, 0, GameImagesMap.getImage("verticalborder"));
		
	
		this.addCollisionable(topBorder);
		this.addCollisionable(rightBorder);
		this.addCollisionable(leftBorder);
		this.addCollisionable(new DeadBorder(this.getLifeCounter(), 800, 19, 0, 580));
		
	}

	private void addPaddle() {
		this.paddle = new NormalPaddle( 400, 550, GameImagesMap.getImage("normalpaddle"));
		this.addCollisionable(this.paddle);
	}
	
	public void replacePaddle(Paddle newPaddle){
		newPaddle.setX(this.getPaddle().getX());
		newPaddle.setY(this.getPaddle().getY());
		if (this.getPaddle().getX() + newPaddle.getDiameter() >= 770){
			newPaddle.setX(769 - newPaddle.getDiameter());
		}

		
		this.removeCollisionable(this.getPaddle());
		this.addCollisionable(newPaddle);
		this.getBall().setStartOn(newPaddle);
		this.setPaddle(newPaddle);
		
	}

	public void ballDroped(NormalBall ball) {
		if (this.lastBall()){
			ball.destroyComponent();
			this.getBallsOnGame().remove(ball);
			this.getLifeCounter().loseLife();
			this.resetPowerUps();
			this.addBall();
		}
		else{
			ball.destroyComponent();
			this.getBallsOnGame().remove(ball);
		}

	}

	private boolean lastBall() {
		return this.getBallsOnGame().size() == 1;
	}

	private void resetPowerUps() {
		this.replacePaddle(new NormalPaddle(0, 0, GameImagesMap.getImage("normalpaddle")));
	}

	public void blockDestroyed(Block block){
		
		this.getScoreGc().add(block.getScore());
		this.setBlockCount(this.getBlockCount() - 1);
		if (this.getBlockCount() == 0){
			this.allBlocksCleared();
		}
	}
	
	protected void allBlocksCleared() {
		
	}

	public void gameOver(){
		this.getGame().setCurrentScene(new GameOverScene(this.getScoreGc().getScore()));
	}

	///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
	public void setPaddle(Paddle paddle) {
		this.paddle = paddle;
	}

	public Paddle getPaddle() {
		return paddle;
	}

	public LifeCounter getLifeCounter() {
		return lifeCounter;
	}

	public void setLifeCounter(LifeCounter lifeCounter) {
		this.lifeCounter = lifeCounter;
	}


	public NormalBall getBall() {
		return this.getBallsOnGame().get(0);
	}

	public ScoreLabel getScoreGc() {
		return scoreGc;
	}

	public void setScoreGc(ScoreLabel scoreGc) {
		this.scoreGc = scoreGc;
	}

	public void setBlockCount(int blockCount) {
		this.blockCount = blockCount;
	}

	public int getBlockCount() {
		return blockCount;
	}

	public List<NormalBall> getBallsOnGame() {
		return ballsOnGame;
	}

	public void setBallsOnGame(List<NormalBall> ballsOnGame) {
		this.ballsOnGame = ballsOnGame;
	}



}
